package com.wgworkshop.pems.service;

import com.wgworkshop.pems.common.BusinessException;
import com.wgworkshop.pems.common.CommonUtils;
import com.wgworkshop.pems.entity.StudentEntity;
import com.wgworkshop.pems.model.StudentModel;
import com.wgworkshop.pems.repository.StudentRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生服务接口实现类。
 * Created by wlc on 2017/7/9.
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;

    @Override
    public StudentModel save(StudentModel studentModel) {
        StudentEntity studentEntity = new StudentEntity();
        BeanUtils.copyProperties(studentModel, studentEntity);
        StudentEntity resultEntity = studentRepository.save(studentEntity);
        StudentModel resultModel = new StudentModel();
        BeanUtils.copyProperties(resultEntity, resultModel);
        return resultModel;
    }

    @Override
    public void delete(Long id) {
        checkExists(id);
        studentRepository.delete(id);
    }

    @Override
    public StudentModel update(Long id, StudentModel studentModel) {
        checkExists(id);

        StudentEntity studentEntity = studentRepository.findOne(id);
        String[] nullProperties = CommonUtils.getNullPropertyNames(studentModel);
        BeanUtils.copyProperties(studentModel, studentEntity, nullProperties);

        StudentEntity resultEntity = studentRepository.save(studentEntity);
        StudentModel resultModel = new StudentModel();
        BeanUtils.copyProperties(resultEntity, resultModel);

        return resultModel;
    }

    @Override
    public StudentModel update(StudentModel studentModel) {
        Long studentId = studentModel.getId();
        checkExists(studentId);

        StudentEntity studentEntity = studentRepository.findOne(studentId);
        BeanUtils.copyProperties(studentModel, studentEntity);

        StudentEntity resultEntity = studentRepository.save(studentEntity);
        StudentModel resultModel = new StudentModel();
        BeanUtils.copyProperties(resultEntity, resultModel);

        return resultModel;
    }

    @Override
    public StudentModel findOne(Long id) {
        StudentEntity studentEntity = studentRepository.findOne(id);
        StudentModel studentModel = null;
        if (studentEntity != null) {
            studentModel = new StudentModel();
            BeanUtils.copyProperties(studentEntity, studentModel);
        }

        return studentModel;
    }

    @Override
    public List<StudentModel> findByNameContaining(String name) {
        String searchName = name != null ? name : "";

        Sort.Order statusOrder = new Sort.Order(Sort.Direction.ASC, "status");
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "code");
        Sort.Order idOrder = new Sort.Order(Sort.Direction.DESC, "id");
        List<Sort.Order> orderList = new ArrayList<>();
        orderList.add(statusOrder);
        orderList.add(codeOrder);
        orderList.add(idOrder);
        Sort sort = new Sort(orderList);
        Iterable<StudentEntity> studentEntities = studentRepository.findByNameContaining(searchName, sort);
        return CommonUtils.convertIterableToList(studentEntities, StudentModel.class);
    }

    @Override
    public boolean exists(Long id) {
        return studentRepository.exists(id);
    }

    @Override
    public void checkExists(Long id) {
        if (!exists(id)) {
            throw new BusinessException(String.format("指定的学生ID不存在[%s]。", id));
        }
    }

    @Override
    public StudentRepository getRepository() {
        return studentRepository;
    }
}
